const express = require('express');
const router = express.Router();
const loginSQL = require('../../db/loginSQL')
const postSQL = require('../../db/postSQL')

const bodyParser = require('body-parser');

// 这是一个删除帖子的路由
router.post('/delpost', bodyParser.json(), (req, res) => {
    // 检查帖子ID是否为空
    if (!req.body["postID"]
        || typeof req.body["postID"] != "number") {
        res.json({
            err: 1,
            message: '帖子ID为空'
        })
    } else if (!req.body["userID"]
        || typeof req.body["userID"] != "number") {
        res.json({
            err: 1,
            message: '帖子ID为空'
        })
    } else {
        var userID = req.body["userID"]
        var postID = req.body["postID"]
        loginSQL.getByID(userID, (result) => {
            var userPower = result[0].userPower
            var userStatus = result[0].userStatus
            if (userStatus == 0) {
                res.json({
                    err: 3,
                    message: '用户被封禁'
                })
            }
            else if (userPower == 0) {
                res.json({
                    err: 2,
                    message: '权限不足',
                    userPower: 0
                })
            }
            else {
                postSQL.delpost(postID, () => {
                    res.json({
                        err: 200,
                        message: '成功删除帖子'
                    })
                })
            }
        })
    }
})

module.exports = router;